package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/11/3
 */
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 后台: 分页查看用户
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity getUsers(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = manageService.getUsers(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询指定用户的详情
     */
    @GetMapping("/users/{id}")
    public ResponseEntity getUserInfo(@PathVariable Long id) {
        UserInfo userInfo = manageService.getUserById(id);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 后台: 分页指定用户发布的视频
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity getVideos(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize,
                                    Long uid) {
        PageResult pageResult = manageService.getVideos(page, pagesize, uid);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询动态
     * @param page
     * @param pagesize
     * @param uid
     * @param state
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   Long uid, String state) {
        state = StringUtils.isEmpty(state) || state.equals("''") ? null : state;
        PageResult pageResult = manageService.getMessages(page, pagesize, uid, state);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据动态id查询动态
     * @param id
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity messagesParticulars(@PathVariable ObjectId id) {
        MovementsVo movementsVo = manageService.messagesParticulars(id);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 根据动态id查看评论
     * @param page
     * @param pagesize
     * @param messageID
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity getComments(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String messageID) {
        PageResult pageResult = manageService.getComments(page, pagesize, new ObjectId(messageID));
        return ResponseEntity.ok(pageResult);
    }


    @PostMapping("/users/freeze")
    public ResponseEntity saveFreeze(@RequestBody Map map) {
        Map massge = manageService.saveFreeze(map);
        return ResponseEntity.ok(massge);
    }

    @PostMapping("/users/unfreeze")
    public ResponseEntity deleteFreeze(@RequestBody Map map) {
        Map massge = manageService.deleteFreeze(map);
        return ResponseEntity.ok(massge);
    }

}
